package uts.sdk.modules.oneoilNewland;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import com.newland.aidl.face.AidlFace;
import com.newland.aidl.scanner.AidlScanner;
import com.newland.aidl.scanner.AidlScannerListener;
import com.newland.aidl.scanner.ScannerCode;
import com.newland.aidl.wxpayface.AidlWXPayFace;
import uts.sdk.modules.oneoilNewland.R;
import uts.sdk.modules.oneoilNewland.NewlandService;
import uts.sdk.modules.oneoilNewland.ICallback;
import uts.sdk.modules.oneoilNewland.Response;

public class ScannerAction {
	private AidlScanner scanner;
	
    private ScannerAction() {
		scanner = NewlandService.getScanner();
	}
    
    // 静态内部类不会在外部类加载时立即加载
    private static class ScannerActionHolder {
        private static final ScannerAction INSTANCE = new ScannerAction();
    }
    
    // 第一次调用时才会加载SingletonHolder类
    public static ScannerAction getInstance() {
        return ScannerActionHolder.INSTANCE;
    }
	
	public void initScanner(Bundle bundle) throws RemoteException {
		scanner.initScan(bundle);
	}
	
	public void startScanner(int scanType, int timeout, ICallback<String> callback) throws RemoteException {
		
		scanner.startScan(scanType, timeout, new AidlScannerListener.Stub(){
			@Override
			public void onScanResult(String barcode) throws RemoteException {
				callback.execute(new Response<String>(1, barcode, ""));
			}

			@Override
			public void onTimeout() throws RemoteException {
				callback.execute(new Response<String>(0, null, "超时"));
			}

			@Override
			public void onError(int errorCode, String detail) throws RemoteException {
				callback.execute(new Response<String>(0, null, detail));
			}

			@Override
			public void onCancel() throws RemoteException {
				callback.execute(new Response<String>(0, null, "取消"));
			}
		});
	}
}